package zhouyang;

import java.util.concurrent.TimeUnit;

/**
 * @author: 叶子
 * @date: 2022/03/04 11:58
 **/
class HoldLockThread implements Runnable{
    private String lockA;
    private String lockB;

    public HoldLockThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"\t自己持有"+lockA+"尝试获得:"+lockB);
            try {
                TimeUnit.SECONDS.sleep(2);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"\t自己持有"+lockB+"尝试获得:"+lockA);
            }
        }
    }
}
public class DeadLockDemo {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";
        new Thread(new HoldLockThread(lockA,lockB),"ThreadAAA").start();
        new Thread(new HoldLockThread(lockB,lockA),"ThreadBBB").start();
/**
 *  linux  ps -ef | grep xxx        ls -l
 *  windows 下的java运行程序，有类似ps的查看进程命令，但目前只需要查看java
 *  jps = java ps                   jps -l
 *  然后再 jstack pid号 定位死锁
 */

    }
}